package com.lch.webdemo.util;


import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Description: DateUtil
 * Author: wangbo
 * Create: 2014-09-03 19:20
 */
public class DateUtil {


    public static final String[] formats = new String[]{"yyyy-MM-dd", "yyyyMMdd", "yyyy.M.d", "yyyy.MM.dd", "yyyy-M-d",
            "yyyy/MM/dd", "yyyy/M/d", "*yyyy/M/d", "*yyyy/MM/dd", "yyyy年M月d日", "yyyy年MM月dd日"};

    /**
     * 使用多个format匹配，其中一个成功即返回
     *
     * @param str
     * @return
     */
    public static Date getDateValueFromString(String str) {
        if (StringUtils.isBlank(str)) {
            return null;
        } else {
            for (int i = 0; i < formats.length; i++) {
                String f = formats[i];
                SimpleDateFormat sf = new SimpleDateFormat(f);
                sf.setLenient(false);
                try {
                    return sf.parse(str);
                } catch (ParseException e) {
                    //e.printStackTrace();
                }
            }
            throw new RuntimeException("日期格式（" + str + "）无法解析");
        }

    }


    /**
     * 获得指定日期的零点零分零秒
     *
     * @param date
     * @return
     */
    public static Date getDateStart(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获得当前日期所在周的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        return c.getTime();
    }

    /**
     * 获得当前日期所在周的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
        return c.getTime();
    }

    /**
     * 获取当前是周几 注意0代表周日 6代表周六
     * @return
     */
    public static int getTodayWeek() {
        Date today = new Date();
        Calendar c=Calendar.getInstance();
        c.setTime(today);
        int week = c.get(Calendar.DAY_OF_WEEK) - 1;
        return week;
    }

    /**
     * 得到几天前的时间
     *
     * @param d
     * @param day
     * @return
     */
    public static Date getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
        return now.getTime();
    }

    /**
     * 得到几天后的时间
     *
     * @param d
     * @param day
     * @return
     */
    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

    /**
     * 返回yyyy-MM-dd格式的日期字符串
     *
     * @param d
     * @return
     */
    public static String getStringDateShort(Date d) {
        if (d == null)
            return null;

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(d);
        return dateString;
    }

    /**
     * 根据yyyy-MM-dd的字符串返回Date对象
     *
     * @param s
     * @return
     */
    public static Date getDateShortFromString(String s) {

        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            return formatter.parse(s);
        } catch (Exception e) {
        }
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
            return formatter.parse(s);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据format的字符串返回Date对象
     *
     * @param s
     * @param format
     * @return
     */
    public static Date getDateByFormatFromString(String s, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        try {
            return formatter.parse(s);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 返回format制定格式的日期字符串
     *
     * @param d
     * @param format
     * @return
     */
    public static String getStringDateByFormat(Date d, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(d);
        return dateString;
    }

    /**
     * 获得n天后的Timestamp
     *
     * @param ts
     * @param n
     * @return
     */
    public static Timestamp getNDaysAfterTimestamp(Timestamp ts, int n) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(ts);
        cal.add(Calendar.DAY_OF_WEEK, n);
        return (new Timestamp(cal.getTime().getTime()));
    }

    public static long getNowTimestamp() {
        return System.currentTimeMillis() / 1000L;
    }

    /**
     * 拿到WEEK数
     *
     * @param date
     * @return
     */
    public static int getWeekOfYear(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setMinimalDaysInFirstWeek(7);
        c.setTime(date);
        return c.get(Calendar.WEEK_OF_YEAR) + 1;
    }

    /**
     * 拿到WEEK数
     *
     * @param date
     * @return
     */
    public static int getWeekOfYear(String date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setMinimalDaysInFirstWeek(7);
        c.setTime(getDateShortFromString(date));
        return c.get(Calendar.WEEK_OF_YEAR) + 1;
    }

    /**
     * 将毫秒数，按照format的格式，转化成日期的字符串
     *
     * @param time
     * @param format
     * @return
     */
    public static String getStringFromLongByFormat(long time, String format) {
        DateFormat formatter = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return formatter.format(calendar.getTime());
    }

    /**
     * 将时间字符串转化成毫秒
     *
     * @param date
     * @param format
     * @return
     * @throws Exception
     */
    public static long getMillisFromStringByFormat(String date, String format) {
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(new SimpleDateFormat(format).parse(date));
            return c.getTimeInMillis();
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 当前日期上或者下时间间隔的的yearMo   interval 负数表示前几月 正数相反
     *
     * @param interval
     * @return
     */
    public static String getYearmoByInterval(int interval) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, interval);

        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        return year + "" + (month < 10 ? "0" + month : month);
    }

    /**
     * 获取给定yearmo的间隔interval(月)的yearmo
     *
     * @param yearmo
     * @param interval
     * @return
     */
    public static String getYearmoByInterval(String yearmo, int interval) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, Integer.parseInt(yearmo.substring(0, 4)));
        cal.set(Calendar.MONTH, Integer.parseInt(yearmo.substring(4, 6)) - 1);
        cal.set(Calendar.DAY_OF_MONTH, 15);
        cal.add(Calendar.MONTH, interval);

        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        return year + "" + (month < 10 ? "0" + month : month);
    }

    /**
     * 获取固定日期上或者下时间间隔的的yearMo   interval 负数表示前几月 正数相反
     *
     * @param interval
     * @return
     */
    public static String getTimeYearMoByInterval(Date date, int interval) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, interval);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        return year + "" + (month < 10 ? "0" + month : month);
    }

    /**
     * 获得日期中的day
     *
     * @return
     */
    public static Integer getDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获得日期中的day，给定日期
     *
     * @param date
     * @return
     */
    public static Integer getDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 截取yyyy-MM-dd 的yearmo
     *
     * @param arg
     */

    public static String getYearmoByStr(String arg) {
        String date[] = arg.split("-");
        return date[0] + date[1];
    }

    /**
     * 获取当前yyyy-MM-dd HH:mm:ss的时间
     *
     * @return
     */
    public static String getNowTime() {
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(date);
        return time;
    }


    //String转Date
    public static Date transferStringtoDate(String strDate, String pattern) {
        if (StringUtils.isEmpty(strDate)) {
            return null;
        }
        DateFormat fmt = new SimpleDateFormat(pattern);
        try {
            Date date = fmt.parse(strDate);
            return date;
        } catch (ParseException e) {
            return null;
        }
    }

    //Date转String
    public static String transferDatetoString(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        DateFormat fmt = new SimpleDateFormat(pattern);
        return fmt.format(date);
    }


    /**
     * 当前月
     *
     * @return
     */
    public static Integer getMonth() {
        Calendar cal = Calendar.getInstance();
        Integer month = cal.get(Calendar.MONTH) + 1;
        return month;
    }

    public static Integer getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH) + 1;
    }

    public static Integer getDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 当前
     *
     * @return
     */
    public static Integer getYmdHms(long timeMillis, int CalendarType) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(timeMillis);
        switch (CalendarType) {
            case Calendar.YEAR:
                return cal.get(CalendarType);
            case Calendar.MONTH:
                return cal.get(CalendarType) + 1;
            case Calendar.DAY_OF_MONTH:
                return cal.get(CalendarType);
            case Calendar.HOUR:
                return cal.get(CalendarType);
            case Calendar.MINUTE:
                return cal.get(CalendarType);
            case Calendar.SECOND:
                return cal.get(CalendarType);
            default:
                return cal.get(CalendarType);
        }
    }

    /**
     * 当前年
     *
     * @return
     */
    public static Integer getYear() {
        Calendar cal = Calendar.getInstance();
        Integer year = cal.get(Calendar.YEAR);
        return year;
    }

    public static Date getMonthFirstDay() {
        String date = getYear() + "-" + getMonth() + "-1";
        Date day = getDateShortFromString(date);
        return day;
    }

    public static Date getYearFirstDay() {
        String date = getYear() + "-1-1";
        Date day = getDateShortFromString(date);
        return day;
    }

    public static Date convertTimestampSecondsToDate(int timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(((long) timestamp) * 1000);
        return calendar.getTime();
    }

    /**
     * 将Date，转化为秒数
     */
    public static int convertDateToTimestampSeconds(Date date) {
        long timeInMillis = date.getTime();
        return (int) (timeInMillis / 1000);
    }

    /**
     * 获取当前时间距离1970.1.1的秒数，对应mysql的unix_timestamp()
     */
    public static int getUnixTimestamp() {
        return (int) (System.currentTimeMillis() / 1000L);
    }

    /**
     * 将秒数，按照format转化为string
     *
     * @param timestamp
     * @param format
     * @return
     */
    public static String convertTimestampSecondsToString(int timestamp, String format) {
        Date date = convertTimestampSecondsToDate(timestamp);
        return getStringDateByFormat(date, format);
    }

    /**
     * 获得指定日期的指定时间
     *
     * @param date
     * @return
     */
    public static Date getDateOnHour(Date date, int hourOfDay) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获得指定日期下一日的指定时间
     *
     * @param date
     * @return
     */
    public static Date getDateNextDayOnHour(Date date, int hourOfDay) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, +1);
        cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获得指定日期前后的日期
     *
     * @param date
     * @return
     */
    public static Date getBeforeOrAfterDate(Date date, int beforeOrAfterDays) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, beforeOrAfterDays);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取sql类型Date
     *
     * @param date
     * @return
     */
    public static java.sql.Date getSqlDateByDate(Date date) {
        String stringDateShort = DateUtil.getStringDateShort(date);
        Date dateShortFromString = DateUtil.getDateShortFromString(stringDateShort);
        return new java.sql.Date(dateShortFromString.getTime());
    }

    /**
     * 获得某年第一天
     *
     * @return
     */
    public static Date getFirstDateOfYear(int year) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(Calendar.YEAR, year);
        return cal.getTime();
    }

    /**
     * 将date对象转化成诸如20160401的字符串，再转化成整数
     *
     * @param date
     * @return
     */
    public static int getDateInt(Date date) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            String dateStr = formatter.format(date);
            return Integer.parseInt(dateStr);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 返回两个日期中较大的一个，如果相等，则任意返回一个
     *
     * @param d1
     * @param d2
     * @return
     */
    public static Date findMax(Date d1, Date d2) {
        if (compare(d1, d2) <= 0) {
            return d2;
        } else {
            return d1;
        }
    }

    /**
     * 返回两个日期中较小的一个，如果相等，则任意返回一个
     *
     * @param d1
     * @param d2
     * @return
     */
    public static Date findMin(Date d1, Date d2) {
        if (compare(d1, d2) <= 0)
            return d1;
        else
            return d2;
    }

    /**
     * 比较两个日期的大小
     */
    public static int compare(Date d1, Date d2) {
        SimpleDateFormat FORMAT = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        String str1 = FORMAT.format(d1);
        String str2 = FORMAT.format(d2);
        return str1.compareTo(str2);
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 计算两个日期机制相差的分钟数
     *
     * @param smdate
     * @param bdate
     * @return
     */
    public static int minutesBetween(Date smdate, Date bdate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_minute = (time2 - time1) / (1000 * 60);
        return Integer.parseInt(String.valueOf(between_minute));
    }

    /**
     * 获取当前年份
     *
     * @return
     */
    public static int getCurrentYear() {
        Calendar cal = Calendar.getInstance();
        Date date = new Date();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);
        return year;
    }

    public static int getCurrentYearmo(){
        int yearmo = Integer.parseInt((DateUtil.getDateInt(DateUtil.getCurrentTime()) + "").substring(0, 6));//当前yearmo
        return yearmo;
    }
    /**
     * 获取指定的月的下一个月字符串，yearmo的格式是201604，返回的格式也是201605
     *
     * @param yearmo
     * @return
     */
    public static String getNextMonth(String yearmo) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, Integer.parseInt(yearmo.substring(0, 4)));
        calendar.set(Calendar.MONTH, Integer.parseInt(yearmo.substring(4, 6)) - 1);
        calendar.add(Calendar.MONTH, 1);
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        return format.format(calendar.getTime());
    }

    /**
     * 将秒数，转化为SQLDate
     *
     * @param timestamp
     * @return
     */
    public static java.sql.Date convertTimestampSecondsToSqlDate(int timestamp) {
        Date date = convertTimestampSecondsToDate(timestamp);
        java.sql.Date sqlDate = new java.sql.Date(date.getTime());
        return sqlDate;
    }

    /**
     * 将诸如20160401的日期字符串，转化成整数
     *
     * @param date
     * @return
     */
    public static int getDateInt(String date) {
        try {
            return Integer.parseInt(date);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 将时间精确到分钟
     *
     * @param date
     * @return
     */
    public static Date correctToMinute(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获得指定时间
     *
     * @param date      日期
     * @param hour      时
     * @param minute    分
     * @param addMinute 增加多少分钟
     * @return
     */
    public static Date getTime(Date date, int hour, int minute, int addMinute) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.MINUTE, addMinute);
        return cal.getTime();
    }


    /**
     * 获得指定时间的指定分钟前后时间
     *
     * @param date
     * @param addMinute
     * @return
     */
    public static Date getTimeAddMinute(Date date, int addMinute) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, addMinute);
        return cal.getTime();
    }

    /**
     * 根据时间戳获取某天的小时数——24小时制
     *
     * @param detailDate 日期
     * @param date       "20160501"格式的日期
     * @return
     */
    public static int getHoursOfDate(Date detailDate, String date) {
        Calendar cal1 = Calendar.getInstance();
        cal1.clear();
        cal1.setTime(detailDate);
        int hour = cal1.get(Calendar.HOUR_OF_DAY);//当天小时
        int date1 = cal1.get(Calendar.DATE);

        Calendar cal2 = Calendar.getInstance();
        cal2.clear();
        cal2.setTime(DateUtil.getDateByFormatFromString(date, "yyyyMMdd"));
        int date2 = cal2.get(Calendar.DATE);
        if (date1 - date2 == 1 && hour == 0) {
            hour = 24;
        }
        return hour;
    }

    /**
     * 获取准确sqldate,下一天0点算前一天24点
     *
     * @return
     */
    public static java.sql.Date getSqlDateWith0Hours(Date date) {
        java.sql.Date sqlDateByDate = getSqlDateByDate(date);
        if (DateUtil.getDateOnHour(sqlDateByDate, 0).compareTo(date) == 0) {
            //零点
            return getSqlDateByDate(getBeforeOrAfterDate(sqlDateByDate, -1));
        }
        return sqlDateByDate;

    }

    public static Date getFirstDayOfYearmo(String yearmo) {
        Calendar cal1 = Calendar.getInstance();
        cal1.clear();
        cal1.set(Calendar.YEAR, Integer.parseInt(yearmo.substring(0, 4)));
        cal1.set(Calendar.MONTH, Integer.parseInt(yearmo.substring(4, 6)) - 1);
        cal1.set(Calendar.DAY_OF_MONTH, 1);
        return getSqlDateByDate(cal1.getTime());
    }

    public static Date getlastDayOfYearmo(String yearmo) {
        Calendar cal1 = Calendar.getInstance();
        cal1.clear();
        cal1.set(Calendar.YEAR, Integer.parseInt(yearmo.substring(0, 4)));
        cal1.set(Calendar.MONTH, Integer.parseInt(yearmo.substring(4, 6)));
        cal1.set(Calendar.DAY_OF_MONTH, 1);

        return getSqlDateByDate(getBeforeOrAfterDate(cal1.getTime(), -1));
    }


    public static Date getCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time = null;
        try {
            time = sdf.parse(sdf.format(new Date()));
            return time;
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date getLastDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        //得到月末
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    /**
     * 计算相差天数
     */
    public static int getDiscrepantDays(Date dateStart, Date dateEnd) {
        return (int) ((dateEnd.getTime() - dateStart.getTime()) / 1000 / 60 / 60 / 24);
    }

    public static Date getLastSunDayOfLastYearmo(String yearmo) {
        Calendar cal1 = Calendar.getInstance();
        cal1.clear();
        cal1.set(Calendar.YEAR, Integer.parseInt(yearmo.substring(0, 4)));
        cal1.set(Calendar.MONTH, Integer.parseInt(yearmo.substring(4, 6)) - 1);
        cal1.add(Calendar.DAY_OF_MONTH, -1);
        cal1.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return getSqlDateByDate(cal1.getTime());

    }

    public static int getNextDateKey(int dateKey) {
        Date date = getDateByFormatFromString(dateKey + "", "yyyyMMdd");
        date = getDateAfter(date, 1);
        return getDateInt(date);
    }

    public static int getDayOfYear(Date date) {
        Calendar cal1 = Calendar.getInstance();
//        cal1.clear();
        cal1.setTime(date);
        return cal1.get(Calendar.DAY_OF_YEAR);
    }


    public static String getFistDayOfYearmo(String yearmo) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, Integer.parseInt(yearmo.substring(0, 4)));
        cal.set(Calendar.MONTH, Integer.parseInt(yearmo.substring(4, 6)) - 1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DAY_OF_MONTH));
        String formatDate = getStringDateByFormat(cal.getTime(), "yyyy-MM-dd");
        String date[] = formatDate.split("-");
        return date[1] + "." + date[2];
    }

    public static String getLastDayOfYearmo(String yearmo) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, Integer.parseInt(yearmo.substring(0, 4)));
        cal.set(Calendar.MONTH, Integer.parseInt(yearmo.substring(4, 6)) - 1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String formatDate = getStringDateByFormat(cal.getTime(), "yyyy-MM-dd");
        String date[] = formatDate.split("-");
        return date[1] + "." + date[2];
    }

    public static Date getLastSundayOfDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (calendar.get(Calendar.DAY_OF_WEEK) == 1)
            calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return getSqlDateByDate(calendar.getTime());
    }

    public static int getLastDayNumOfYearmo(String yearmo) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, Integer.parseInt(yearmo.substring(0, 4)));
        cal.set(Calendar.MONTH, Integer.parseInt(yearmo.substring(4, 6)) - 1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String formatDate = getStringDateByFormat(cal.getTime(), "yyyy-MM-dd");
        String date[] = formatDate.split("-");
        return Integer.parseInt(date[2]);
    }

    public static String getStringDateByFormat(int year, int month, int day, String format) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, day);
        return DateUtil.getStringDateByFormat(cal.getTime(), format);
    }

    public static int getYearFromYearmo(String yearmo) {
        return Integer.parseInt(yearmo.substring(0, 4));
    }

    public static int getMonthFromYearmo(String yearmo) {
        return Integer.parseInt(yearmo.substring(4, 6));
    }

    public static java.sql.Date getSqlDateFromStringShort(String dateStr) {
        if (dateStr == null) {
            return null;
        }

        Date date = DateUtil.getDateShortFromString(dateStr);
        if (date == null) {
            return null;
        }

        return new java.sql.Date(date.getTime());
    }


}
